﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace BlueportCoffee.LabelingModle
{
    public class KLabelLib
    {
        public const int COM_PORT = 0x02;
        public const int USB_PORT = 0x02;
        public const int LPT_PORT = 0x03;
        public const int PRN_PORT = 0x04;
        public const int TCP_PORT = 0x05;

        public const int CMD_T_TSPL = 0x01;
        public const int CMD_T_JPL = 0x02;

        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint EnumPrinterPorts(int portType, byte[] buffer, int length);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern long GetPortHandler(int portType, String portName);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern long OpenPort(long handle);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void ClosePrinterPort(long handle);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void RelasePortHandler(long handle);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void SetCommandType(long handle, int commandType);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern bool SendPrintCmd(long handle);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void ClearPrintCmd(long handle);


        /* TSPL command */
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetLabelSize(long handle, int paperWidth, int papwerHeight);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetLabelVertDistance(long handle, int m, int n);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetLabelShift(long handle, int shiftVal);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetReference(long handle, int referenceX, int referenceY);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintSpeed(long handle, int printSpeed);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintDensity(long handle, int printDensity);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetDirection(long handle, int direction);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetClearGraphCache(long handle, int isClear);

        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintText(long handle, int x, int y, String fontName, int rotation, int xMultiplication, int yMultiplication, String text);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintBARCode(long handle, int x, int y, String codeType, int height, int readable, int rotation, int narrow, int wide, String codeData);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintQRCode(long handle, int x, int y, int ECCLevel, int CellWidth, int mode, int rotation, String codeData);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintBitmap(long handle, int printX, int printY, byte[] bitmapFullPath, int nBinaryAlgorithm);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetReversePrintRegion(long handle, int printX, int printY, int printXWdith, int printYHeight);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetPrintCount(long handle, int printCopyCnt);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void TSPL_SetCut(long handle);


        /* JPL command */

        //JPL rotate type
        public const int JPL_LABEL_ROTATE_0 = (0x00);
        public const int JPL_LABEL_ROTATE_90 = (0x01);
        public const int JPL_LABEL_ROTATE_180 = (0x02);
        public const int JPL_LABEL_ROTATE_270 = (0x03);

        //JPL text font type
        public const int JPL_FONT_NORMAL = (0x00);
        public const int JPL_FONT_ROTATE_90 = (0x10);
        public const int JPL_FONT_BOLD = (0x01);
        public const int JPL_FONT_UNDER_LINE = (0x02);
        public const int JPL_FONT_BOLD_UNDER_LINE = (0x03);
        public const int JPL_FONT_INVERSE = (0x04);

        public const int JPL_FONT_SIZE_1 = (0x1100);
        public const int JPL_FONT_SIZE_2 = (0x2200);
        public const int JPL_FONT_SIZE_3 = (0x3300);
        public const int JPL_FONT_SIZE_4 = (0x4400);
        public const int JPL_FONT_SIZE_5 = (0x5500);
        public const int JPL_FONT_SIZE_6 = (0x6600);

        public const int JPL_SHOW_TYPE_INVERSE = (0x01);
        public const int JPL_SHOW_TYPE_ROTATE_0 = (0x00);
        public const int JPL_SHOW_TYPE_ROTATE_90 = (0x02);
        public const int JPL_SHOW_TYPE_ROTATE_180 = (0x04);
        public const int JPL_SHOW_TYPE_ROTATE_270 = (0x06);

        public enum E_BARCODE_TYPE
        {
            BAR_CODE_T_UPC_A = 0,
            BAR_CODE_T_UPC_E = 1,
            BAR_CODE_T_EAN13 = 2,
            BAR_CODE_T_EAN8 = 3,
            BAR_CODE_T_CODE39 = 4,
            BAR_CODE_T_I25 = 5,
            BAR_CODE_T_CODABAR = 6,
            BAR_CODE_T_CODE93 = 7,
            BAR_CODE_T_CODE128 = 8,
            BAR_CODE_T_CODE11 = 9,
            BAR_CODE_T_MSI = 10,
            BAR_CODE_T_128M = 11,
            BAR_CODE_T_EAN128 = 12,
            BAR_CODE_T_25C = 13,
            BAR_CODE_T_39C = 14,
            BAR_CODE_T_39 = 15,
            BAR_CODE_T_EAN13_2 = 16,
            BAR_CODE_T_EAN13_5 = 17,
            BAR_CODE_T_EAN8_2 = 18,
            BAR_CODE_T_EAN8_5 = 19,
            BAR_CODE_T_POST = 20,
            BAR_CODE_T_UPC_A_2 = 21,
            BAR_CODE_T_UPC_A_5 = 22,
            BAR_CODE_T_UPC_E_2 = 23,
            BAR_CODE_T_UPC_E_5 = 24,
            BAR_CODE_T_CPOST = 25,
            BAR_CODE_T_MSIC = 26,
            BAR_CODE_T_PLESSEY = 27,
            BAR_CODE_T_ITF14 = 28,
            BAR_CODE_T_EAN14 = 29,

        };

        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetLabelStart(long handle, int x, int y, int width, int height, int rotate);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetPrintText(long handle, int x, int y, int fontType, String text);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetPrintLine(long handle, int startX, int startY, int endX, int endY, int width, int color);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetPrintRectangle(long handle, int left, int top, int right, int bottom, int width, int color);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetPrintBARCode(long handle, int x, int y, int barCodeType, int barCodeTypeHeight, int unitWidth, int rotation, String codeData);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetPrintQRCode(long handle, int x, int y, int version, int ECCLevel, int unitWidth, int rotation, String codeData);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetPrintBitmap(long handle, int printX, int printY, int showType, byte[] bitmapFullPath, int nBinaryAlgorithm);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetLabelEnd(long handle);
        [DllImport("KLabelLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void JPL_SetLabelPrint(long handle, int printCopyNum);
    }
}
